using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using DecisionMaking.BusinessLogic;
using DecisionMaking.BusinessLogic.Model;
using MvcContrib;

namespace DecisionMaking.WebApp
{
	[HandleError]
	public class CueAttributesController : BaseController
	{
		/// <summary>
		/// Injected by Windsor 
		/// </summary>
		public IEnvironmentManager EnvironmentManager {private get; set;}
		
		[HttpGet]
		//TODO secure
		public ActionResult List(int id)
		{
			ViewData["cueAttributes"] = EnvironmentManager.GetCueAttributes(id);
			
			// pre-set the environment id for the new cue attributes form model
			return View("List", new CueAttributeModel{EnvironmentId = id});
		}
		
		[HttpPost]
		//TODO secure
		public ActionResult Add(CueAttributeModel model)
		{
			try 
			{
				EnvironmentManager.AddCueAttribute(model);
				return this.RedirectToAction(c => c.List(model.EnvironmentId));
			} 
			catch (DuplicateCueAttributeNameException e) 
			{
				ModelState.AddModelError("Name", "A cue attribute with this name already exists");
			}	
			
			return View(model);
		}
		
		[HttpPost]
		//TODO secure
		public ActionResult Delete(int id)
		{
			EnvironmentManager.RemoveCueAttribute(id);
			
			return Json(new {success = true, deletedId = id});
		}		
	}
}

